package com.spica.spicaschool.ui.consult

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import com.drakeet.multitype.ItemViewDelegate
import com.spica.spicaschool.R
import com.spica.spicaschool.bean.diary.Diary
import com.spica.spicaschool.databinding.ItemConsultBinding
import com.spica.spicaschool.ui.main.MainFragmentDirections

/**
 * 问答列表
 */

class ConsultDelegate(val activity: Activity) :
  ItemViewDelegate<Diary, ConsultDelegate.ViewHolder>() {

  class ViewHolder(val viewBinding: ItemConsultBinding) :
    RecyclerView.ViewHolder(viewBinding.root)

  @SuppressLint("SetTextI18n")
  override fun onBindViewHolder(holder: ViewHolder, item: Diary) {
    with(holder.viewBinding) {
      tvAuthor.text = item.user.uNickname
      tvContent.text = item.diaryContent
      ivHead.load(item.user.uAvatar) {
        placeholder(R.drawable.default_head)
        error(R.drawable.default_head)
        transformations(CircleCropTransformation())
      }
      tvTitle.text = item.diaryTitle
      tvReadNum.text = "${item.diaryReadNum}人阅读过"
      root.setOnClickListener {
        //点击进入详情页面
        activity.findNavController(R.id.fragment_container_view)
          .navigate(MainFragmentDirections.actionHomeToConsultGroupFragment())
      }
    }
  }

  override fun onCreateViewHolder(context: Context, parent: ViewGroup): ViewHolder {
    val viewBinding = ItemConsultBinding.inflate(LayoutInflater.from(context), parent, false)
    return ViewHolder(viewBinding)
  }
}